package com.atguigu.jxc.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.jxc.dao.DamageListGoodsDao;
import com.atguigu.jxc.dao.GoodsTypeDao;
import com.atguigu.jxc.dao.PurchaseListGoodsDao;
import com.atguigu.jxc.dao.SupplierDao;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.PurchaseListGoodsService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: PurchaseListGoodsServiceImpl
 * Description:
 * Date: 2022/1/17 15:21
 *
 * @version:
 * @since JDK 1.8
 */
@Service
public class PurchaseListGoodsServiceImpl implements PurchaseListGoodsService {

    @Autowired
    private PurchaseListGoodsDao purchaseListGoodsDao;

    @Autowired
    private SupplierDao supplierDao;

    @Autowired
    private GoodsTypeDao goodsTypeDao;





    @Override
    public void savePurchaseListGoods(PurchaseList purchaseList, String purchaseListGoodsStr, HttpSession session) {

        User user = (User)session.getAttribute("currentUser");
        purchaseList.setUserId(user.getUserId());
        purchaseListGoodsDao.savePurchaseList(purchaseList);





        List<PurchaseListGoods> purchaseListGoodsList = JSONObject.parseArray(purchaseListGoodsStr, PurchaseListGoods.class);

        for (PurchaseListGoods purchaseListGoods : purchaseListGoodsList) {
            purchaseListGoods.setPurchaseListId(purchaseList.getPurchaseListId());
            purchaseListGoodsDao.savePurchaseListGoods(purchaseListGoods);
        }

    }


    @Override
    public List<PurchaseList> getPurchaseListGoods(String purchaseNumber, Integer supplierId,Integer state, String sTime, String eTime) {
        List<PurchaseList> purchaseListList = purchaseListGoodsDao.getPurchaseListGoods(purchaseNumber,supplierId,state,sTime,eTime);
        return purchaseListList;
    }

    @Override
    public List<PurchaseListGoods> getGoodsList(Integer purchaseListId) {
        List<PurchaseListGoods>purchaseListGoodsList = purchaseListGoodsDao.getGoodsList(purchaseListId);
        return purchaseListGoodsList;
    }


    @Override
    public void deletePurchaseListGoods(Integer purchaseListId) {
        purchaseListGoodsDao.deletePurchaseListGoods(purchaseListId);
        purchaseListGoodsDao.deletePurchaseList(purchaseListId);
    }

    @Override
    public void updateState(Integer purchaseListId) {
        purchaseListGoodsDao.updateState(purchaseListId);
    }

    @Override
    public List<PurchaseListGoodsCount> count(String sTime, String eTime, Integer goodsTypeId, String codeOrName) {

        List<Integer> listGoodsIds = purchaseListGoodsDao.getGoodsList1(goodsTypeId,codeOrName);

        List<PurchaseList> purchaseListList= purchaseListGoodsDao.getPurchaseListGoods(null, null,null,sTime,eTime);
        ArrayList<PurchaseListGoodsCount> purchaseListGoodsCountArrayList = new ArrayList<>();
        for (PurchaseList purchaseList : purchaseListList) {
            String supplierName = supplierDao.selectById1(purchaseList.getSupplierId());
            List<PurchaseListGoods> purchaseListGoodsList = purchaseListGoodsDao.getGoodsList(purchaseList.getPurchaseListId());
            for (PurchaseListGoods purchaseListGoods : purchaseListGoodsList) {

                String goodsTypeName = goodsTypeDao.selectById(purchaseListGoods.getGoodsTypeId());

                PurchaseListGoodsCount purchaseListGoodsCount = new PurchaseListGoodsCount();

                purchaseListGoodsCount.setPurchaseListGoodsId(purchaseListGoods.getPurchaseListGoodsId());
                purchaseListGoodsCount.setNumber(purchaseList.getPurchaseNumber());
                purchaseListGoodsCount.setSupplierName(supplierName);
                purchaseListGoodsCount.setDate(purchaseList.getPurchaseDate());
                purchaseListGoodsCount.setCode(purchaseListGoods.getGoodsCode());
                purchaseListGoodsCount.setName(purchaseListGoods.getGoodsName());
                purchaseListGoodsCount.setModel(purchaseListGoods.getGoodsModel());
                purchaseListGoodsCount.setGoodsType(goodsTypeName);
                purchaseListGoodsCount.setUnit(purchaseListGoods.getGoodsUnit());
                purchaseListGoodsCount.setPrice(purchaseListGoods.getPrice());
                purchaseListGoodsCount.setNum(purchaseListGoods.getGoodsNum());
                purchaseListGoodsCount.setTotal(purchaseListGoods.getTotal());
                purchaseListGoodsCountArrayList.add(purchaseListGoodsCount);
            }
        }


        ArrayList<PurchaseListGoodsCount> purchaseListGoodsCountArrayList1 = new ArrayList<>();
        for (Integer listGoodsId : listGoodsIds) {
            for (PurchaseListGoodsCount purchaseListGoodsCount : purchaseListGoodsCountArrayList) {
                if(listGoodsId.equals(purchaseListGoodsCount.getPurchaseListGoodsId())){
                    purchaseListGoodsCountArrayList1.add(purchaseListGoodsCount);
                }
            }
        }
        return purchaseListGoodsCountArrayList1;

    }
}
